package com.wuge.gulimall.product.service.impl;

import com.wuge.gulimall.product.entity.AttrEntity;
import com.wuge.gulimall.product.service.AttrAttrgroupRelationService;
import com.wuge.gulimall.product.service.AttrService;
import com.wuge.gulimall.product.vo.AttrGroupWithAttrVo;
import com.wuge.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.product.dao.AttrGroupDao;
import com.wuge.gulimall.product.entity.AttrGroupEntity;
import com.wuge.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        if (catId == 0) {
            return queryPage(params);
        } else {
            QueryWrapper<AttrGroupEntity> attrGroupEntityQueryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId);

            String key = (String) params.get("key");
            if (!StringUtils.isEmpty(key)) {
                attrGroupEntityQueryWrapper.and((qy) -> {
                    qy.like("attr_group_name", key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    attrGroupEntityQueryWrapper
            );

            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrVo> getlistAttrGroupwithattr(Long catelogId) {
        QueryWrapper<AttrGroupEntity> attrGroupEntityQueryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
        List<AttrGroupEntity> list = list(attrGroupEntityQueryWrapper);
        List<AttrGroupWithAttrVo> collect = list.stream().map((entity) -> {
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(entity, attrGroupWithAttrVo);
            List<Long> attrIds = attrAttrgroupRelationService.getAttrIds(entity.getAttrGroupId());
            List<AttrEntity> attrs = attrService.listByIds(attrIds);
            attrGroupWithAttrVo.setAttrs(attrs);

            return attrGroupWithAttrVo;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<SpuItemAttrGroupVo> getSpuAttr(Long spuId, Long catalogId) {
        AttrGroupDao baseMapper = getBaseMapper();
        List<SpuItemAttrGroupVo> list= baseMapper.getAttrs(spuId,catalogId);
        return list;
    }
}